<?php

namespace common\models;

use Yii;

class Areas extends \yii\db\ActiveRecord
{
    public static function getDb()
    {
        return Yii::$app->get("common_db");
    }
    
    public static function tableName()
    {
        return '{{%areas}}';
    }


    /*取得所有省数据*/
    public static function getProvinceList()
    {
        $list = self::find()->where(['parentid'=>0, 'level'=>1])->asArray()->all();
        return $list;
    }

    /*根据 省ID 取得对应的城市数据*/
    public static function getCityByProvince($provinceid)
    {
        $list = self::find()->where('parentid = :id', [':id' => $provinceid])->asArray()->all();
        if(!is_null($list)){
            return $list;
        }
        return [];
    }

    /*根据 省市ID 取得对应的区数据*/
    public static function getDistrictByCity($cityid)
    {
        $list = self::find()->where('parentid = :id', [':id' => $cityid])->asArray()->all();
        if(!is_null($list)){
            return $list;
        }
        return [];
    }

    /*根据ID，取出地区名称*/
    public static function getAreaName($id)
    {
        if(!empty($id)){
            $data = self::find()->select(['areaname', 'shortname'])->where('id = :id', [':id' => $id])->asArray()->one();
            if(!empty($data)){
                return $data;
            }
        }
        return '';
    }
    
}
